
// Bus.c

#ifndef		BUS_C
#define		BUS_C

#include 	"Bus.h"

uint8_t BusOpen(struct sBus *this, uint8_t dir, uint8_t value) {
    BusWrite(this, value);
    BusDir(this, dir);
    return 0;
}

uint8_t BusClose(struct sBus *this) {
    BusDir(this, BUS_INPUT);
    BusWrite(this, BUS_LOW);
    return 0;
}

uint8_t BusRead(struct sBus *this) {
    if ((*this->tris >> this->pin0) & 0x01) {
        return (*this->port >> this->pin0) & 0xFF;
    } else {
        return (*this->lat >> this->pin0) & 0xFF;
    }
    return 0;

}

void BusWrite(struct sBus *this, uint8_t value) {
    *this->port = (*this->lat & ~(0xFF << this->pin0)) | (value << this->pin0);
    return;
}

void BusDir(struct sBus *this, uint8_t dir) {
    if (dir) {
        *this->tris |= (0x00FF << this->pin0);
    } else {
        *this->tris &= ~(0x00FF << this->pin0);
    }
    return;
}

#endif		//#ifndef		BUS_C
